package yana.tpe.onlineshop.services.impl;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import yana.tpe.onlineshop.domain.dao.CategoryDao;
import yana.tpe.onlineshop.domain.domains.Category;
import yana.tpe.onlineshop.services.CategoryService;

/**
 *
 */
//@Service
@Transactional
public class CategoryServiceImpl extends AbstractServiceImpl<Integer, Category>
        implements CategoryService {

    private static final Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Autowired
    private CategoryDao categoryDao;

    @Override
    protected CategoryDao getDao() {
        return categoryDao;
    }

    @Override
    @Transactional
    public void move(Integer newParentId, Integer categoryId) {
        logger.debug("move categroi [id={}] to [id={}]", categoryId, newParentId);
        getDao().move(newParentId, categoryId);
    }

    @Override
    @Transactional
    public List<Category> getCategoryWithParents(Integer categoryId) {
        logger.debug("getCategoryWithParents(categoryId={})", categoryId);
        
        List<Category> categoryWithParents = getDao().getCategoryWithParents(categoryId);
        
        logger.debug("{}", categoryWithParents);
        
        return categoryWithParents;
    }

    @Override
    @Transactional
    public List<Category> getChildCategoris(Category parent) {
        Integer parentId = parent != null ? parent.getId() : null;
        logger.debug("getChildCategoris(parentId={})", parentId);
        
        List<Category> childCategoris = getDao().getChildCategoris(parentId);
        
        logger.debug("{}", childCategoris);
        
        return childCategoris;
    }

    @Override
    @Transactional
    public List<Category> getCategoryForEdit() {

        logger.debug("getCategoryForEdit");

        List<Category> categoryForEdit = getDao().getCategoryForEdit();

        logger.debug("{}", categoryForEdit);

        return categoryForEdit;
    }

    @Override
    @Transactional
    public void addCategory(Integer parentId, Category category) {
        logger.debug("Try to add category [{}], to category [id={}]", category, parentId);

        Category parent = get(parentId);

        logger.debug("Parent category is [{}]", parent);
        
        category.setParent(parent);
        save(category);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        logger.debug("Try to delete category [id={}]", id);
        getDao().delete(id);
    }

    @Override
    @Transactional
    public void updateCategoryName(Integer categoryId, String newName) {
        logger.debug("Try to update category [id={}], set name to [{}]", categoryId, newName);

        Category category = get(categoryId);
        logger.debug("Category is [{}]", category);

        category.setName(newName);
        saveOrUpdate(category);
    }
}
